package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.IssueCreateBO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.MarketIssue;
import com.cskaoyan.bean.vo.IssueListVO;
import com.cskaoyan.service.IssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商场管理-通用问题
 * 创建日期: 2022/04/07 20:10
 *
 * @author ChengWenqi
 */

@RestController
@RequestMapping("admin/issue")
public class MarketIssueController {

    @Autowired
    IssueService issueService;

    // 通用问题 admin/issue/list
    @RequestMapping("list")
    public BaseRespVo issueList(BaseParam param, String question) {
        IssueListVO issueListVO = issueService.queryIssueList(param, question);
        return BaseRespVo.ok(issueListVO);
    }


    // 添加新问题及回复 admin/issue/create
    // 参数校验：问题及回复内容不能空串、空格、null
    @RequestMapping("create")
    public BaseRespVo issueCreate(@RequestBody @Validated IssueCreateBO issueCreateBO) {
        MarketIssue newIssue = issueService.issueCreate(issueCreateBO);
        return BaseRespVo.ok(newIssue);
    }

    // 更新问题及回复 admin/issue/update
    // 参数校验：问题及回复内容不能空串、空格、null，不允许修改提问内容，只能修改回复
    @RequestMapping("update")
    public BaseRespVo issueUpdate(@RequestBody @Validated MarketIssue issue) {
        MarketIssue newIssue = issueService.issueUpdate(issue);
        // 规定：如果提问与原来的不一样，不允许修改，返回null
        if (newIssue == null) {
            return BaseRespVo.fail(10000, "不能修改提问！");
        }
        // 规定：如果根据id没找到记录，返回的pojo的id设为0
        if (newIssue.getId() == 0) {
            return BaseRespVo.fail(10000, "记录不存在！");
        }
        // 修改成功
        return BaseRespVo.ok(newIssue);
    }

    // 删除问题记录 admin/issue/delete （逻辑删除）
    @RequestMapping("delete")
    public BaseRespVo issueDelete(@RequestBody @Validated MarketIssue issue) {
        int code = issueService.issueDelete(issue);
        if (code == 200) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail(10000, "记录不存在！");
    }
}
